package id.co.gomobile.assessment

class User {
	
	static transients = ['administrator', 'deletable']
	                     
	// associations
	static hasMany = [roles: Role]
    static belongsTo = Role

	// properties
	String username
	String password
	String email
	boolean enabled = true
	
	// password properties
	boolean administrator = false
	
	// associated domain
	Profile profile = new Profile()

	// date properties
	Date dateCreated
	Date lastUpdated
	
	// constraints
    static constraints = {
		username(size: 4..64, matches: "[a-zA-Z0-9]*", blank: false, unique: true)
		password(blank: false)
        email(email: true, blank: false, unique: true)
        enabled()
		dateCreated()
		lastUpdated()
    }
	
	public boolean isAdministrator() {
		def adminRole = this.roles.find { it.name == "ROLE_ADMIN" }
		return(adminRole != null)
	}
	
	public boolean isDeletable() {
		return this.id == 1
	}

}
